package com.kedsoft.andaily.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.kedsoft.andaily.common.CommonList;
import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Router;
import com.kedsoft.andaily.service.RouterService;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-10
 */
@Controller
@RequestMapping("/router")
public class RouterController extends BaseController{

    @Autowired
    RouterService routerService;

    @RequestMapping("index")
    public String index(){
        return "router/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Router> getPageList(Pagination<Router> pagination, @RequestParam(required = false) String name){
        Map<String, Object> query = new HashMap<>();
        query.put("name", name);
        routerService.pageList(pagination,query);
        PageVo<Router> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<Router> allList(){
        EntityWrapper<Router> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        return routerService.selectList(entityWrapper);
    }

    @RequestMapping(value = "allList")
    @ResponseBody
    public CommonList<Router> allSelectList(){
        EntityWrapper<Router> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        List<Router> list = routerService.selectList(entityWrapper);
        return CommonList.success(list);
    }

    @RequestMapping(value = "form")
    public String form(){
        return "router/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Router param){
        routerService.insert(param);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Router param){
        routerService.updateById(param);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        routerService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            routerService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "router/search";
    }

}

